﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using MSDNWebcast.Utils;
using System.Windows.Media.Imaging;
using System.Windows.Interop;
using System.Windows;

namespace MSDNWebcast.Models
{
    /// <summary>
    /// 文件类型
    /// </summary>
    public class FileType
    {

        public FileType()
        {
            CanPlay = false;
        }

        private BitmapSource icon;

        /// <summary>
        /// 文件类型ID
        /// </summary>
        public string ID { get; set; }

        /// <summary>
        /// 能否播放
        /// </summary>
        public bool CanPlay { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 文件扩展名
        /// </summary>
        public string FileExtension { get; set; }

        /// <summary>
        /// 文件图标
        /// </summary>
        public BitmapSource Icon
        {
            get
            {
                if (icon == null)
                {
                    var fileIcon = RegisteredFileType.GetIconByFileType(IntPtr.Zero, FileExtension);
                    if (fileIcon != null)
                    {
                        var image = fileIcon.ToBitmap();
                        var source = Imaging.CreateBitmapSourceFromHBitmap(image.GetHbitmap(), IntPtr.Zero,
                            Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(image.Width, image.Width));
                    }
                }
                return icon;
            }
        }


        public override bool Equals(object obj)
        {
            if (obj != null && obj is FileType && !ReferenceEquals(obj, this))
                return ((FileType)obj).ID == this.ID;
            return base.Equals(obj);
        }

        public override int GetHashCode()
        {
            return ID.GetHashCode();
        }
    }
}
